﻿
@using IOA.MES.DataAccess.Enum
@{
    ViewBag.Title = "锁定管理";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";

    var workshops = ViewBag.Workshops as List<IOA.MES.DataAccess.VModel.VWorkshop>;
}
@section Contents{
    <style>
        .layui-form-label {
            width: auto;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.WorkShop">车间：</label>
                        <div class="layui-input-inline">
                            <select lay-filter="selWorkshops" name="workShopCode">
                                <option value="">所有车间</option>
                                @foreach (var item in workshops)
                                {
                                    <option value="@item.WorkshopCode">@item.WorkshopName</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.OrderNo">订单号：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="orderNo" placeholder="请输入订单号" value="@ViewBag.OrderNo" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.WorkorderNo">工单号：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="workOrderNo" placeholder="请输入工单号" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.BarCode">组件条码：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="barCode" placeholder="请输入组件条码" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.CreateDate">创建日期：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtRangeDate" placeholder="请选择创建日期" value="@(DateTime.Today.AddDays(-7).ToString("yyyy-MM-dd")+" 至 "+DateTime.Today.AddDays(1).ToString("yyyy-MM-dd"))" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.UnLockStatus">解锁状态：</label>
                        <div class="layui-input-inline">
                            <select lay-filter="selReviewed" name="reviewed">
                                <option value="">所有状态</option>
                                <option value="1">已解锁</option>
                                <option value="0">未解锁</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn layuiadmin-btn-list" type="submit" lay-submit lay-filter="submit">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                        <button id="btnBatchLock" class="layui-btn layui-btn-warm layuiadmin-btn-list" type="submit" lay-submit lay-filter="submit">
                            <i class="layui-icon layui-icon-screen-restore layuiadmin-button-btn"></i>批量锁定
                        </button>
                        <button id="btnBatchUnLock" class="layui-btn layui-btn-normal layuiadmin-btn-list" type="submit" lay-submit lay-filter="submit">
                            <i class="layui-icon layui-icon-password layuiadmin-button-btn"></i>批量解锁
                        </button>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card">
            <div class="layui-card-body">
                <table id="table" lay-filter="table"></table>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });
        setTimeout(function () {
            initLayui();
        }, 50);

        function initLayui() {
            layui.use(['table', 'layer', 'form','laydate'], function () {
                var layer = layui.layer,
                    table = layui.table,
                    form = layui.form,
                    laydate = layui.laydate;
                laydate.render({
                    elem: '#txtRangeDate'
                    , range: '至'
                });
                var options = {
                    elem: '#table'
                    , url: '/Quality/QueryLocks'
                    , method: "post"
                    , where: { workshopCode: "", orderNo: "", workOrderNo: "", barCode: "", startTime: "@DateTime.Today.AddDays(-7).ToString("yyyy-MM-dd")", endTime: "@DateTime.Today.AddDays(1).ToString("yyyy-MM-dd")", reviewed: null, result: null }
                    , page: {
                        limit: 15
                        , limits: [15, 20, 25, 30, 40, 50]
                    }
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { title: '序号', type: "numbers", width: 60, fixed: 'left' }
                        , { field: 'BarCode', title: '组件条码', minWidth: 200, fixed: 'left' }
                        , { field: 'WorkOrderNo', title: '工单号', minWidth: 200, fixed: 'left' }
                        , { field: 'OrderNo', title: '订单号', minWidth: 150, fixed: 'left' }
                        , { field: 'WorkshopName', title: '车间', minWidth: 120 }
                        , { field: 'ProductCode', title: '成品料号', minWidth: 150 }
                        , { field: 'ProductName', title: '组件名称', minWidth: 150 }
                        , { field: 'CNCurSite', title: '当前站点', minWidth: 120 }
                        , { field: 'CNLockSite', title: '锁定站点', minWidth: 120 }
                        , { field: 'CNLockedBy', title: '锁定人', minWidth: 80 }
                        , { field: 'LockedTimeStr', title: '锁定时间', minWidth: 150 }
                        , { field: 'LockedReason', title: '锁定说明', minWidth: 200 }
                        , { field: 'CNUnLockedBy', title: '解锁人', minWidth: 80 }
                        , { field: 'UnLockedTimeStr', title: '解锁时间', minWidth: 150 }
                        , { field: 'UnLockedReason', title: '解锁说明', minWidth: 250 }
                        , {
                            title: '解锁状态', minWidth: 90, fixed: 'right', templet: function (d) {
                                return "<span style='color:" + (d.UnLockedBy > 0 ? "#009688" : "#FFB800") + ";'>" + (d.UnLockedBy > 0 ? "已解锁" : "未解锁") + "</span>";
                            }
                        }
                        , {
                            title: '操作', width: 100, fixed: 'right', templet: function (d) {
                                var str = d.UnLockedBy > 0 ? '' : '<button class="layui-btn layui-btn-sm layui-btn-warm" onclick="review(\'' + d.BarCode + '\')"><i class="layui-icon">&#xe673;</i>解锁</button>';
                                return str;
                            }
                        }
                    ]]
                };

                function queryTable(data) {
                    options.page.curr = 1;
                    options.where = data;

                    if (table.index <= 0) {
                        table.render(options);
                    } else {
                        table.reload("table", options);
                    }
                }

                form.on('submit(submit)', function (data) {
                    console.log(data.field);
                    var forms = data.field;
                    var rangeTime = $("#txtRangeDate").val().trim() == '' ? new Array() : $("#txtRangeDate").val().trim().split(' 至 ');
                    forms.startDate = rangeTime.length <= 0 ? null : rangeTime[0];
                    forms.endDate = rangeTime.length <= 0 ? null : rangeTime[1];
                    forms.result = forms.result == "" ? null : parseInt(forms.result);
                    forms.reviewed = forms.reviewed == "" ? null : parseInt(forms.reviewed) == 1;
                    queryTable(forms);
                    return false;
                });
                $("#btnSearch").click();

                $("#btnBatchUnLock").click(function () {
                    layer.open({
                        type: 2,
                        shade: [0.1, '#000'],
                        area: ['600px', '550px'],
                        content: "/Quality/BatchUnLock",
                        end: function () {
                            $("#btnSearch").click();
                        }
                    });
                });

                $("#btnBatchLock").click(function () {
                    layer.open({
                        type: 2,
                        shade: [0.1, '#000'],
                        area: ['600px', '600px'],
                        content: "/Quality/BatchLock",
                        end: function () {
                            $("#btnSearch").click();
                        }
                    });
                });
            });
        }

        function review(barCode) {
            layer.prompt({
                formType: 2,
                title: '请输入解锁说明',
                area: ['800px', '350px'],
                yes: function (index, layero) {
                    var value = layero.find(".layui-layer-input").val().trim();
                    var loadId = layer.load();
                    $.ajax({
                        url: "/Quality/UnLock",
                        data: {
                            barCode: barCode,
                            unLockReason: value
                        },
                        complete: function () {
                            layer.close(loadId);
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            layer.msg("解锁成功！", { icon: 6, time: 1000 });
                            $("#btnSearch").click();
                        }
                    });
                    layer.close(index);
                }
            });
        }
    </script>
}
